#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from src.db import db_meizi

__author__ = 'DB_BOY'

import requests
from bs4 import BeautifulSoup

url = "http://www.mzitu.com/all"
url2 = "http://www.mzitu.com/old"


class Meizi(object):
    def __init__(self, url, title):
        self.url = url
        self.title = title


''' 获取html页面'''


def downHtml(url):
    headers = {
        'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10240",
        'Connection': 'Keep-Alive',
        'Referer': "http://www.mzitu.com/99566"
    }
    return requests.get(url, headers=headers).text


''' 解析获取所有的妹子url  '''


def parseHtml(url):
    html =  downHtml(url)
    soup = BeautifulSoup(html)
    list = soup.select("a[target='_blank']")
    count = 0  # 添加一个统计链接的数量
    for a in list:
        title = a.text
        if len(title.strip()) == 0:
            title = a.find('img').get('alt')
        url =a["href"].strip()
        info = Meizi(url, title.strip())
        row =write_info(info)
        if row ==0:
            break


'''-------------写入列表'''

def write_info(info):
    connection = db_meizi().connectDB()
    try:
        # 获得数据库游标（游标提供了一种对从表中检索出的数据进行操作的灵活手段，就本质而言，
        # 游标实际上是一种能从包括多条数据记录的结果集中每次提取一条记录的机制。
        # 游标总是与一条SQL 选择语句相关联因为游标由结果集（可以是零条、一条或由相关的选择语句检索出的多条记录）
        # 和结果集中指向特定记录的游标位置组成。）
        with connection.cursor() as cursor:
            sql = 'select count(*) from pic_list where list_url =%s'
            cursor.execute(sql, (info.url))
            result = cursor.fetchone()
            count = result[0]
            if (count > 0):
                print("--------------更新: ", info.url, info.title)
                # 事务提交
                cursor.close()
                return 0
                # sql = 'update pic_list set file_path=%s where leet_no =%s'
                # cursor.execute(sql, (info.path, info.id))
            else:
                print("**************插入: ", info.url, info.title)
                sql = 'insert into pic_list(list_url, list_title) values(%s, %s)'
                # 执行sql语句
                cursor.execute(sql, (info.url, info.title))
                connection.commit()
                # 事务提交
                cursor.close()
                return 1
    finally:
        # 关闭数据库连接
        connection.close()
        # print("-------------写入数据库结束--------------")


if __name__ == "__main__":
    # 获取所有妹子的url
    parseHtml(url)
    parseHtml(url2)
